package david.java.tools;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

/**
 * @Description: 基于jdk8 的时间工具类, 是线程安全的
 * @Author: David
 * @Date: Create in 上午11:26 2021/4/2
 */
public class DateUtil {

    private final static DateTimeFormatter FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    /**
     * 功能描述: 字符串时间 转 时间戳
     *
     * @param time
     * @return:
     */
    public static long string2Timestamp(String time) {
        LocalDateTime parse = LocalDateTime.parse(time, FORMAT);
        return LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    /**
     * 功能描述: 时间戳(毫秒) 转 String
     *
     * @param time
     * @return:
     */
    public static String timestamp2String(long time) {
        return FORMAT.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()));
    }

    /**
     * 功能描述: 时间戳(秒) 转 String
     *
     * @param time
     * @return:
     */
    public static String second2String(long time) {
        return FORMAT.format(LocalDateTime.ofInstant(Instant.ofEpochSecond(time), ZoneId.systemDefault()));
    }

    /**
     * 功能描述: 判断时间1 是否在时间2 之前
     *
     * @param time1
     * @param time2
     * @return:
     */
    public static boolean isBefore(String time1, String time2) {
        LocalDateTime t1 = LocalDateTime.parse(time1, FORMAT);
        LocalDateTime t2 = LocalDateTime.parse(time2, FORMAT);
        return t1.isBefore(t2);
    }


    /**
     * 功能描述: 当前时间戳, 秒
     *
     * @param
     * @return:
     */
    public static long currentSecond() {
        return Instant.now().getEpochSecond();
    }

    /**
     * 功能描述: 当前时间戳, 毫秒
     *
     * @param
     * @return:
     */
    public static long currentMillSecond() {
        return Instant.now().toEpochMilli();
    }

    /**
     * 功能描述: 返回指定日期与当日相差的天数
     *
     * @param year
     * @param month
     * @param day
     * @return:
     */
    public static long fromDate2Now(int year, int month, int day) {
        LocalDate now = LocalDate.now();
        LocalDate target = LocalDate.of(year, month, day);
        return ChronoUnit.DAYS.between(target, now);
    }

    /**
     * 功能描述: 返回指定日期与当日相差的天数
     *
     * @param year
     * @param month
     * @param day
     * @return:
     */
    public static long fromDate2Now(int year, int month, int day, int tarYear, int tarMonth, int tarDay) {
        LocalDate now = LocalDate.now();
        LocalDate source = LocalDate.of(year, month, day);
        LocalDate target = LocalDate.of(tarYear, tarMonth, tarDay);
        return ChronoUnit.DAYS.between(source, target);
    }

    public static void main(String[] args) {


        LocalDate d = LocalDate.of(2023, 5, 1);
        System.out.println(d.minusDays(1));
        System.out.println(d.minusDays(2));
        System.out.println(d.minusDays(42));

        // System.out.println(fromDate2Now(2023, 5, 1));
        // System.out.println(fromDate2Now(2023, 5, 1, 2022,9,23));
    }

}
